(function(){
var DELTA_NUM = 5;
var m_hDoit = 0;
var m_currentVote = 1;
var m_currentNote = null;
//
init();
//
function init(){
    var target = document.getElementById("ex-select-sceond");
    var button = document.getElementById("ex-get-all");
    //------- change current page parameters -------
    // window.limit = 1000000;
    changeSpeed();
    findOutFirst();
    target.addEventListener("change", changeSpeed, false); 
    button.addEventListener("click", getAllVotes, false); 
}

function findOutFirst(){
    var list = window.uuidA;
    var maxScore = 0;
    var index = 0;
    clearFirstFlag();
    for(var i=0; i<list.length; i++){
        var score = parseInt($("#dvote_"+list[i])[0].innerText);
        if(score > maxScore){
            maxScore = score;
            index = i;
        }
        // console.log("score", score);
    }
    // console.log("dvote", $("#dvote_"+list[index])[0]);
    //
    m_currentNote = $("#dvote_"+list[index])[0];
    if(m_currentNote){
        m_currentNote.style["background"] = "red";
    }
}

function clearFirstFlag(){
    if(m_currentNote){
        m_currentNote.style["background"] = null;
    }  
}

function getAllVotes(){
    console.log("getAllVotes");
    clearFirstFlag();
    Ijax.request("http://vote.video.iqiyi.com/api/get.php",{
    GET : {
        uuid : uuid
    },
    onSuccess : function(res){
        for(i=0;i<uuidA.length;i++)
        {
            $("#dvote_"+uuidA[i])[0].innerHTML = res[i][uuidA[i]] ? res[i][uuidA[i]] : 0;
        }
        findOutFirst();
    },
    onError : function(res){
        if (res.code == 'A00001')
        {
            for(i=0;i<uuidA.length;i++)
            {
                $("#dvote_"+uuidA[i])[0].innerHTML = "-1";
            }
        }
 
    }
})
}

function changeSpeed(){
    var target = document.getElementById("ex-select-sceond");
    // console.log(target);
    var interval = target.options[target.selectedIndex].value;
    interval = parseInt(interval);
    console.log("interval", interval);
    clearTimeout(m_hDoit);
    doit(interval);
}

function doit(interval){
    var num = interval + Math.round(DELTA_NUM * Math.random());
    var str = "vote " + m_currentVote + " toady,at " + new Date();
    var target = document.getElementById("ex-info");
    cvote2('201210291_4');
    console.log(str);
    target.innerText = str;
    findOutFirst();
    m_currentVote++;
    clearTimeout(m_hDoit);
    console.log("num", num);
    m_hDoit = setTimeout(arguments.callee, num * 1000, interval);
}

function cvote2(uuid){
    var target = document.getElementById("ex-info");
    Ijax.request("http://vote.video.iqiyi.com/api/add.php?v=" + new Date().getTime(),{
        GET : {
            uuid : uuid
        },
        onSuccess : function(){
            $("#dvote_"+uuid)[0].innerHTML = parseInt($("#dvote_"+uuid)[0].innerHTML)+1;
        },
        onError : function(res){
            target.innerText = "[Error!] " + res.code;
            console.log(res);
            if (res.code == 'A00001'){
                // alert('参数错误');
                return false;
            }else if (res.code == 'A00002'){
                // alert("投票每天限制"+limit+"次，感谢您的参与！");
                return false;
            }
        }
    })
}

}).call(this);
